package l744.GuanDao;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

public class GuanDaoMain extends JPanel{
    private Bird bird=new Bird();
    public static final int WIDTH = 1300;
    public static final int HEIGHT = 413;
    private UpPipeline[] upPipelines = {};
    private int GAMEEND=1;
    private int upEnterIndex=0;
    public  void UpPipelinesEnterAction(){
        upEnterIndex++;
        if (upEnterIndex%100==0){
            UpPipeline upPipeline=new UpPipeline();
            upPipelines= Arrays.copyOf(upPipelines,upPipelines.length+1);
            upPipelines[upPipelines.length-1]=upPipeline;
        }
    }

    public void BirdDieAction(){
        for (UpPipeline g : upPipelines) {
            if (bird.isHit(g)) {
                GAMEEND = 0;
            }
        }
    }
    public void outOfGuanDaoAction() {
        for (int i = 0; i < upPipelines.length; i++) {
            if (upPipelines[i].inOutOfGuanDao()) {
                upPipelines[i] = upPipelines[upPipelines.length - 1];
                upPipelines = Arrays.copyOf(upPipelines, upPipelines.length - 1);
            }
        }
    }
    public void birdDown(){
        Bird bird=new Bird();
        bird.y=bird.y-bird.speed;
    }
    public void action(){
        Timer timer=new Timer();
        int interval=10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (GAMEEND==1){
                    UpPipelinesEnterAction();
                    for (UpPipeline upPipeline : upPipelines) upPipeline.move();
                    birdDown();
                    bird.move();
                    outOfGuanDaoAction();
                    repaint();
                    BirdDieAction();
                }
            }
        }, interval, interval);
        KeyAdapter keyAdapter=new KeyAdapter() {
            int sw=1;
            public void keyPressed(KeyEvent e) {

                if (e.getKeyCode()==KeyEvent.VK_SPACE&&sw==1){
                    bird.jump();
                    sw=0;
                }
            }
            public void keyReleased(KeyEvent e){
                if (e.getKeyCode()==KeyEvent.VK_SPACE){
                    sw=1;
                }
            }
        };
        this.addKeyListener(keyAdapter);
    }
    @Override
    public void paint(Graphics g) {
        GuanDaoPng.BackGround.paintIcon(null,g,0,0);
        bird.paintImage(g);
        for (UpPipeline upPipeline : upPipelines) {
            upPipeline.paintImage(g);
        }
        if (GAMEEND==0){
            GuanDaoPng.End.paintIcon(null,g,WIDTH/2-320,HEIGHT/2-160);
        }
    }
    //主函数
    public static void main(String[] args) {
        JFrame frame=new JFrame();
        GuanDaoMain guanDaoMain=new GuanDaoMain();
        guanDaoMain.setFocusable(true);
        frame.add(guanDaoMain);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("管道夹小鸟");
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);//调用paint()方法
        guanDaoMain.action();
    }
}